package com.zhangh.summer.admin.support.util;

import javax.crypto.Cipher;
import java.security.GeneralSecurityException;
import java.security.KeyFactory;
import java.security.spec.EncodedKeySpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.crypto.encodings.PKCS1Encoding;

/**
 * @description: TODO
 * @author: zhanghui  zhanghlucky@163.com
 * @create: 2021-10-09 21:18
 **/
public class RSAUtil {
    //PKCS#1 PEM/Base64
    private static String publicKey =
            "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCdF/POCPax5aV8CM/rlMi3ZGrQ" +
                    "xxGeA18euyLfhrnuzYb7apXNezZq+Q4bTeISJAAaweYW27thI75jWZQpbAme5a5N" +
                    "PBgjNaxteQhFvIkpqyf0dgBY+wRoyVJ6chdmAmLhWN345jNmbI0+iJnLS69qvbeL" +
                    "YGqwdMlWRk1W+FEIxwIDAQAB" ;

    private static String privateKey =
            "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAJ0X884I9rHlpXwI" +
                    "z+uUyLdkatDHEZ4DXx67It+Gue7Nhvtqlc17Nmr5DhtN4hIkABrB5hbbu2EjvmNZ" +
                    "lClsCZ7lrk08GCM1rG15CEW8iSmrJ/R2AFj7BGjJUnpyF2YCYuFY3fjmM2ZsjT6I" +
                    "mctLr2q9t4tgarB0yVZGTVb4UQjHAgMBAAECgYBjiyH6lMN9xr8t8YePCCCH1SG6" +
                    "WepX7lr/5iUa7wwyvhmihs+V/VVmQ5skwh1W0Wkfi255XZNG4pdp7QFtngAZqbZj" +
                    "hoRe73hQb3Cx8fd6qETOou3gmPSGIkzfLSKuki/Vi9x474NotCBLPhmCLmJ7WYqW" +
                    "yoQ7EGo2YHXlDZRswQJBAM9ULigy+8MXG8mjN76NIBNY6kyfVsk5DQ8Ym61j8uY5" +
                    "y/vJG56LwEbGjcKARKB1nLlaqVCfPAOE2E6oSaQPATsCQQDB+MnURykzBPiryaao" +
                    "j2o0cKfHEGd/PQmfk6pufCS3d39reChQgUechfE9alp7dYQRPz30mlnVGaTP3p6L" +
                    "L93lAkA2yUHhMYfBx8t5/v1uBxDgy/AuCzT9u2ut/4yzxuOa3s7+W+hGfxtyGGnB" +
                    "ae+4jxu5BzISseFD9MjMEEBVb/mdAkB7yCYUHy09b6i5B8AgxRSDTnji4acKXYss" +
                    "mHEkfHh50/ouPMmNRlLKofcmuB3xtzaGQxNoaq90/B5ewLPwIqW5AkAZOLpm3Iyn" +
                    "F6ou2E2P+lqFkZh3jy5CI0iRIanJeUoaS4rIJk1JiDBlulhXHqVbsJ9mbd2Pkqt6" +
                    "BUTVzOfz1NUw" ;

    public static String encrypt(String data) throws GeneralSecurityException {
        return encrypt(data,publicKey);
    }

    public static String encrypt(String data, String publicKey) throws GeneralSecurityException {

        EncodedKeySpec keySpec = new X509EncodedKeySpec(base64Decode(publicKey));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");

        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, keyFactory.generatePublic(keySpec));
        return base64Encode(cipher.doFinal(data.getBytes()));
    }

    public static String decrypt(String data) throws GeneralSecurityException {
        return decrypt(data,privateKey);
    }
    public static String decrypt(String data, String privateKey) throws GeneralSecurityException {

        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(base64Decode(privateKey));
        //PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKey.getBytes());
        //PKCS1Encoding keySpec=new PKCS1Encoding(base64Decode(privateKey));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");

        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.DECRYPT_MODE, keyFactory.generatePrivate(keySpec));
        return new String(cipher.doFinal(base64Decode(data)));
    }

    static String base64Encode(byte[] bytes) {

        return Base64.encodeBase64String(bytes);
    }

    static byte[] base64Decode(String str) {

        return Base64.decodeBase64(str);
    }
}
